package org.liuh.designs.spider;

import lombok.NoArgsConstructor;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.liuh.designs.spider.dtos.AddressNode;
import org.liuh.designs.spider.utils.HttpClientUtil;
import org.liuh.designs.spider.utils.SpiderHttpUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;

/**
 * @ClassName: AreaSpiderCountyTask
 * @Description:
 * @Author: liuh
 * @Date: 2020/9/30 14:55
 * @Version: 1.0
 */
@NoArgsConstructor
public class AreaSpiderCountyTask implements Runnable {

    // 待处理数量(node)..
    private CopyOnWriteArrayList waitDealList;

    private AddressNode countyNode;

    private CountDownLatch countyDown;

    private AreaConstant.HttpUtilEnum utilEnum;

    public AreaSpiderCountyTask(AddressNode countyNode, CountDownLatch countyDown, AreaConstant.HttpUtilEnum utilEnum, CopyOnWriteArrayList<AddressNode> list) {
        this.countyNode = countyNode;
        this.countyDown = countyDown;
        this.utilEnum = utilEnum;
        this.waitDealList = list;
    }

    /**
     * @Description: 根据区域爬街道信息....
     * @Author: liuh
     * @Date: 2020/10/8
     */
    @Override
    public void run() {
        try {
            if (countyNode.getLevel() == 4) {
                waitDealList.add(countyNode);
                return;
            }
            String townUrl = countyNode.getNodeUrl();
            String sourceUrl = countyNode.getParentNode().getNodeUrl();
            if (townUrl == null) {
                waitDealList.add(countyNode);
                return;
            }
            String tmpTown = null;
            if (utilEnum == AreaConstant.HttpUtilEnum.NORMAL) {
                tmpTown = SpiderHttpUtils.sendGet(false, townUrl, null, null, AreaConstant.CHAR_SET);
            } else if (utilEnum == AreaConstant.HttpUtilEnum.HTTP_CLIENT) {
                tmpTown = HttpClientUtil.doHttpGet(townUrl, sourceUrl, null, AreaConstant.CHAR_SET);
            }
            if (tmpTown == null) {
                waitDealList.add(countyNode);
                return;
            }
            Document townPage = Jsoup.parse(tmpTown);
            if (townPage == null) {
                waitDealList.add(countyNode);
                return;
            }
            Elements townTable = townPage.getElementsByClass(AreaConstant.getNameByClass(AreaConstant.ClassEnum.town, AreaConstant.TagEnum.table));
            if (townTable == null || townTable.isEmpty()) {
                waitDealList.add(countyNode);
                return;
            }
            Elements townTrs = townTable.first().getElementsByClass(AreaConstant.getNameByClass(AreaConstant.ClassEnum.town, AreaConstant.TagEnum.tr));
            if (townTrs == null || townTrs.isEmpty()) {
                waitDealList.add(countyNode);
                return;
            }
            List<AddressNode> townNodes = new ArrayList<>();
            String townCode;
            for (Element townTr : townTrs) {
                Elements tds = townTr.getElementsByTag(AreaConstant.TagEnum.td.name());
                if (tds == null || tds.isEmpty()) {
                    continue;
                }
                Element codeElementTd = tds.get(0);
                //code
                townCode = codeElementTd.getElementsByTag(AreaConstant.TagEnum.a.name()).text();
                if (townCode == null || townCode.isEmpty()) {
                    continue;
                }
                AddressNode townNode = new AddressNode();
                Element countyLinkA = codeElementTd.getElementsByTag(AreaConstant.TagEnum.a.name()).first();
                if (countyLinkA != null) {
                    String countyHref = countyLinkA.attr(AreaConstant.AttrEnum.href.name());
                    townNode.setNodeUrl(AreaConstant.getFullLinkUrl(townUrl, countyHref, AreaConstant.ClassEnum.town));
                }
                //name
                Element countyLinkB = tds.get(1).getElementsByTag(AreaConstant.TagEnum.a.name()).first();
                if (countyLinkB == null) {
                    countyLinkB = tds.get(1);
                }
                String townname = countyLinkB.text();
                townNode.setAddressCode(townCode);
                townNode.setLevel(AreaConstant.LevelEnum.LEVEL_4.getLevel());
                townNode.setAddressName(townname);
                townNode.setParentNode(countyNode);
                townNodes.add(townNode);
            }
            countyNode.setChildNodeList(townNodes);
            System.out.println(String.format("%s， 处理完毕！-- 街道数量:%s, countDown: %s", countyNode.getAddressName(), townNodes.size(), countyDown.getCount()));
        } catch (Exception ex) {
            waitDealList.add(countyNode);
            ex.printStackTrace();
            System.out.println(String.format("区县：%s, code: %s 获取街道异常；IP异常, 请尝试切换使用代理Ip ", countyNode.getAddressName(), countyNode.getAddressCode()));
        } finally {
            countyDown.countDown();
        }
    }
}